#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Any, Dict, Optional


class BaseAppException(Exception):
    """应用程序基础异常类"""
    
    def __init__(
        self,
        message: str,
        code: str = "INTERNAL_ERROR",
        status_code: int = 500,
        details: Optional[Dict[str, Any]] = None
    ):
        self.message = message
        self.code = code
        self.status_code = status_code
        self.details = details or {}
        super().__init__(self.message)
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            "error": {
                "code": self.code,
                "message": self.message,
                "details": self.details
            }
        }


class DatabaseError(BaseAppException):
    """数据库错误"""
    
    def __init__(
        self,
        message: str,
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code="DATABASE_ERROR",
            status_code=500,
            details=details
        )


class VectorDBError(BaseAppException):
    """向量数据库错误"""
    
    def __init__(
        self,
        message: str,
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code="VECTOR_DB_ERROR",
            status_code=500,
            details=details
        )


class EmbeddingError(BaseAppException):
    """向量嵌入错误"""
    
    def __init__(
        self,
        message: str,
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code="EMBEDDING_ERROR",
            status_code=500,
            details=details
        )


class LLMError(BaseAppException):
    """语言模型错误"""
    
    def __init__(
        self,
        message: str,
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code="LLM_ERROR",
            status_code=500,
            details=details
        )


class NotFoundError(BaseAppException):
    """资源未找到错误"""
    
    def __init__(
        self,
        message: str,
        resource_type: str,
        resource_id: str,
    ):
        super().__init__(
            message=message,
            code="NOT_FOUND",
            status_code=404,
            details={
                "resource_type": resource_type,
                "resource_id": resource_id
            }
        )


class ValidationError(BaseAppException):
    """输入验证错误"""
    
    def __init__(
        self,
        message: str,
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code="VALIDATION_ERROR",
            status_code=400,
            details=details
        )


class SQLGenerationError(BaseAppException):
    """SQL生成错误"""
    
    def __init__(
        self,
        message: str,
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code="SQL_GENERATION_ERROR",
            status_code=500,
            details=details
        )